<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>通读缓存回调</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="memcached.callbacks.result.html">结果回调</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="memcached.sessions.html">Sessions支持</a></div>
 <div class="up"><a href="memcached.callbacks.html">回调</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="memcached.callbacks.read-through" class="section">
  <h2 class="title">通读缓存回调</h2>
  <p class="para">
  通读缓存回调在一个元素没有从服务端检索到的时候被调用。这个回调函数会接收到Memcached对象，请求的key以及
  一个引用方式传递的值变量等三个参数。此回调函数负责通过返回true或false来决定在key没有值时设置一个默认值。
  如果回调返回true，Memcached会存储&quot;传出参数&quot;(引用传递的值变量)存储的值到memcached服务端并将其返回到原来
  的调用函数中。仅仅<span class="methodname"><a href="memcached.get.html" class="methodname">Memcached::get()</a></span>和<span class="methodname"><a href="memcached.getbykey.html" class="methodname">Memcached::getByKey()</a></span>
  支持这类回调，因为Memcache协议不支持在请求多个key时提供未检索到key的信息。
  </p>
  <div class="example" id="example-4334">
   <p><strong>Example #1 通读回调示例</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$m&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Memcached</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addServer</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$profile_info&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'user:'</span><span style="color: #007700">.</span><span style="color: #0000BB">$user_id</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'user_info_cb'</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">user_info_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$memc</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$user_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;从数据库读取个人信息&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;...&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$profile_info</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="memcached.callbacks.result.html">结果回调</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="memcached.sessions.html">Sessions支持</a></div>
 <div class="up"><a href="memcached.callbacks.html">回调</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
